#!/usr/bin/env python
# -*- coding:utf-8 -*-

# file:manage.py
# author:JackieX
# datetime:2022-02-06 14:28
# software: PyCharm
"""
   应用启动的入口程序   python manage.py runserver
"""

from app import create_app
from flask_script import Manager, Server
from flask import request, g, jsonify
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address

from app.utils.response_code import RET
from app.utils.loggings import loggings

# 创建flask的app对象(利用工厂模式);
# 创建对象时，传入此时配置环境参数：develop;
app = create_app("develop")
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024  # 设置最大请求体大小为 16MB
limiter = Limiter(app, key_func=get_remote_address, default_limits=["200 per day", "50 per hour"])
manager = Manager(app)
manager.add_command("runserver", Server(use_debugger=True))
# @app.before_request
# def user_validation():
#     print(request.endpoint)  # 方便跟踪调试

#     if not request.endpoint:  # 如果请求点为空
#         return jsonify(code=RET.URLNOTFOUND, message="url not found", error="url not found")

#     # 请求点白名单
#     permission = ["ssoUser.user_login", "apiversion.apiversion", "ssoUser.ssoUser"]

#     # 如果不是请求上述列表中的接口，需要验证token
#     if request.endpoint not in permission:
#         # 在请求头上拿到token
#         token = request.headers.get("Token")
#         if not all([token]):
#             return jsonify(code=RET.PARAMERR, message="缺少参数Token或请求非法")

#         # 校验token格式正确与过期时间
#         s = Serializer(app.config["SECRET_KEY"])
#         try:
#             g.user = s.loads(token)
#         except Exception as e:
#             loggings.exception(1, e)
#             # 单平台用户登录失效
#             return jsonify(code=RET.SESSIONERR, message='用户未登录或登录已过期')

if __name__ == "__main__":
    manager.run()
